#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define maxVertices 100

typedef struct EdgeNode{
    int adjVex;//边所指向的VNode
    struct EdgeNode *next;
}EdgeNode;
typedef struct VNode{
    char data;
    int indegree;
    EdgeNode *firstedge;
}VNode,AdjList[maxVertices];
typedef struct GraphAdjList{
    AdjList vertices;
    int numEdges,numVertices;
}GraphAdjList;
void CreateGraph(GraphAdjList *G){//无向图出度和入度相等
    int i,j,k;
    EdgeNode *p;
    printf("请输入顶点数和边数：");
    scanf("%d%d",&G->numVertices,&G->numEdges);
    printf("请输入顶点信息：");
    for(i=0;i<G->numVertices;i++){
        getchar();
        scanf("%c",&G->vertices[i].data);
        G->vertices[i].firstedge=NULL;
    }
    printf("请输入边信息i j(表示i->j):\n");
    for(k=0;k<G->numEdges;k++){
        scanf("%d%d",&i,&j);
        p=(EdgeNode*)malloc(sizeof(EdgeNode));
        p->adjVex=j;//存储弧头节点（也就是箭头所指向的节点下标
        G->vertices[j].indegree++;
        p->next=G->vertices[i].firstedge;//头插法
        G->vertices[i].firstedge=p;
        //无向图
        p=(EdgeNode*)malloc(sizeof(EdgeNode));
        p->adjVex=i;
        G->vertices[i].indegree++;
        p->next=G->vertices[j].firstedge;//头插法
        G->vertices[j].firstedge=p;
    }
}
void printGraph(GraphAdjList G){
    printf("邻接表为:\n");
    for(int i=0;i<G.numVertices;i++){
        EdgeNode *p=G.vertices[i].firstedge;
        printf("(%d)%c",G.vertices[i].indegree,G.vertices[i].data);
        while(p){
            printf("->%c->",G.vertices[p->adjVex].data);
            p=p->next;
        }
        printf("\n");
    }
}
int main(){
    GraphAdjList G;
    CreateGraph(&G);
    printGraph(G);
    return 0;
}